<html>
<head>
		<script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDfP_-B0UmijgRbJD1M784b0dtsWuh-yzM&libraries=places&sensor=false" type="text/javascript"></script>
		<script src="./json2.js" type="text/javascript"></script>
		<script type="text/javascript">
			var map;
			var placeService;
			var geocoder;
			var radius = 500;
			
			function initialize()
			{
				var options = {
					center: new google.maps.LatLng(49.279,-123.116),
					zoom: 3,
					mapTypeId: getGoogleMapTypeId("Roadmap"),
					disableDoubleClickZoom: true
				};

				// Create a new Google map object
				map = new google.maps.Map(document.getElementById("map"), options);
				geocoder = new google.maps.Geocoder();
				
				placeService = new google.maps.places.PlacesService(map);
				
				google.maps.event.addListener(map, 'dblclick', function (event) {
					var circleOptions = {
						strokeColor: "#FF0000",
						strokeOpacity: 0.8,
						strokeWeight: 2,
						fillColor: "#FF0000",
						fillOpacity: 0.35,
						map: map,
						center: event.latLng,
						radius: radius
					};
					
					cityCircle = new google.maps.Circle(circleOptions);
				
					var request = {
						location: event.latLng,
						radius: radius,
						types: ['atm']
					};
					
					placeService.search(request, placeSearchCallback);
				});
				
			}
			
			function placeSearchCallback(results, status)
			{
				if (status == google.maps.places.PlacesServiceStatus.OK)
				{
					for (var i = 0; i < results.length; i++)
					{
						var place = results[i];
						createMarker(place);
					}
				}
			}
			
			function createMarker(placeResult)
			{
				var image = new google.maps.MarkerImage(placeResult.icon);
				image.scaledSize = new google.maps.Size(40, 40);
				
				var options = {
					animation: google.maps.Animation.BOUNCE,
					map: map,
					position: placeResult.geometry.location,
					icon: image,
					title: placeResult.name
				};
				
				var marker = new google.maps.Marker(options);
				
				var contentString = "<h4>" + placeResult.name + "</h4>" + "<h5>Rating:</h5>&nbsp;" + placeResult.rating + "<h5>types:</h5>&nbsp;" + placeResult.types;
				
				var infowindow = new google.maps.InfoWindow({
					content: contentString,
					maxWidth: 200
				});
				
				google.maps.event.addListener(marker, 'click', function() {
				  infowindow.open(map, marker);
				});
			}
			
			function getGoogleMapTypeId(input)
			{
				if (input == "Roadmap")
					return google.maps.MapTypeId.ROADMAP;
				else if (input == "Hybrid")
					return google.maps.MapTypeId.HYBRID;
				else if (input == "Satellite")
					return google.maps.MapTypeId.SATELLITE;
				else if (input == "Terrain")
					return google.maps.MapTypeId.TERRAIN;
				return google.maps.MapTypeId.ROADMAP;
			}
			
			function getMapType(input)
			{
				if (input == google.maps.MapTypeId.ROADMAP)
					return "Roadmap";
				else if (input == google.maps.MapTypeId.HYBRID)
					return "Hybrid";
				else if (input == google.maps.MapTypeId.SATELLITE)
					return "Satellite";
				else if (input == google.maps.MapTypeId.TERRAIN)
					return "Terrain";
				return "Roadmap";
			}

			function makeHttpRequest(url, method, body, token)
			{
				var client = new XMLHttpRequest();
				client.open(method, url, false);
				if (token != null)
					client.setRequestHeader("X-SAP-LogonToken", token);
				client.setRequestHeader("Access-Control-Allow-Origin", "*");
				client.setRequestHeader("Content-Type", "application/json");
				client.setRequestHeader("Accept", "application/json");
				client.send(body);

				if (client.status == 200)
					return client;
				else
				{
					alert("failed: " + client.statusText);
					return null;
				}
			}
			
			function logonToBOE()
			{
				var url = "http://win2k8r2:6405/biprws/logon/long";
				var request = makeHttpRequest(url, "GET", "", null);
				if (request == null)
					return null;
				
				// now try to logon
				var userLogon = JSON.parse(request.responseText);
				var element = document.getElementById("output");
				userLogon['userName'] = "Administrator";
				userLogon['password'] = "Password1";

				
				request = makeHttpRequest(url, "POST", JSON.stringify(userLogon), null);
				if (request == null)
					return null;

				var token = request.getResponseHeader("X-SAP-LogonToken");
		        var html = "out: " + token;
				element.innerHTML = html;

				url = "http://win2k8r2:6405/biprws/infostore";
				makeHttpRequest(url, "GET", "", token);
			}

			function makeHttpRequestBase64(url, method, body, token)
			{
				var client = new XMLHttpRequest();
				client.open(method, url, false);
				client.setRequestHeader("Authorization", "Basic " + token);
				client.setRequestHeader("Access-Control-Allow-Origin", "*");
				client.setRequestHeader("Content-Type", "application/json");
				client.setRequestHeader("Accept", "application/json");
				client.send(body);

				if (client.status == 200)
					return client;
				else
				{
					alert("failed: " + client.statusText);
					return null;
				}
			}
			
			function logonToBOE2()
			{
				initialize();
				
				var result;
				var url = "http://win2k8r2:6405/biprws/infostore";
				var base64logon = "QWRtaW5pc3RyYXRvcjpQYXNzd29yZDE=";
				var request = makeHttpRequestBase64(url, "GET", "", base64logon);
				if (request == null)
					return null;
				
				var reportUrl = "http://win2k8r2:6405/biprws/infostore/8923/rpt/data.svc/COUNTRY";
				
				
				request = makeHttpRequestBase64(reportUrl, "GET", "", base64logon);

				result = JSON.parse(request.responseText);

				var groups = result['d']['results'];
				var country;
				for (var i = 0; i < groups.length; i++)
				{
					country = groups[i]['Name'];
					if (i == 0)
						createMarker(country);
				}

		        var html = "out: " + result['d']['results'][0]['Name'];
			}

			function createMarker(countryName)
			{
				geocoder.geocode({ address: countryName }, function (geocoderResults, status) {
					if (status == google.maps.GeocoderStatus.OK)
					{
						var circleOptions = {
								strokeColor: "#FF0000",
								strokeOpacity: 0.8,
								strokeWeight: 2,
								fillColor: "#FF0000",
								fillOpacity: 0.35,
								map: map,
								center: geocoderResults[0].geometry.location,
								radius: 500
							};
							
						cityCircle = new google.maps.Circle(circleOptions);
						var html = geocoderResults[0].geometry.location;
				        var element = document.getElementById("output");
						element.innerHTML = html;
					}
				});
			}

		</script>
</head>
<body onLoad="logonToBOE2()">
	<div id="map" style="width: 100%; height: 90%;"></div>
	<div id="output"></div>
</body>
</html>